/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-27 下午12:24 Created by dwzhang
 */
package com.jade.modules.cms.impl;

import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.modules.cms.CmsException;
import com.jade.modules.cms.ContentCount;
import com.jade.modules.cms.ContentCountDao;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;

/**
 * JPA方式的接口。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-1-27 下午12:24
 */
//@Named ("module_cmsCountDao")
public class JpaCountDao implements ContentCountDao
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    @Cacheable (value = "module.cms.ccount.g", key = "#entityType + '_' + #entityId")
    public ContentCount getCount(String entityType, long entityId)
    {
        String sql = SQL_COUNT + " WHERE entityType=:etype AND entityId=:eid";
        Query query = entityManager.createQuery(sql);
        query.setParameter("etype", entityType);
        query.setParameter("eid", entityId);
        return SqlUtils.getSingleResult(query);
    }

    @Caching (evict = {@CacheEvict (value = "module.cms.ccount.g", key = "#count.entityType + '_' + #count.entityId"),
            @CacheEvict (value = "module.cms.ccount.l", allEntries = true)})
    public void setCount(ContentCount count, boolean increase)
            throws CmsException
    {
        String sql1 = "UPDATE ContentCount SET commentCount=:cc,downloadCount=:dc, browserCount=:bc," +
                "dislikeCount=:dlc,likeCount=:lc,score=:s,reserve1=:r1,reserve2=:r2 WHERE entityType=:etype AND entityId=:eid";
        String sql2 = "UPDATE ContentCount SET commentCount=:cc+commentCount,downloadCount=:dc+downloadCount, " +
                "browserCount=:bc+browserCount," + "dislikeCount=:dlc+dislikeCount,likeCount=:lc+likeCount," +
                "score=:s+score,reserve1=:r1+reserve1,reserve2=:r2+reserve2 WHERE entityType=:etype AND entityId=:eid";
        Query query;
        if (increase) {
            query = entityManager.createQuery(sql2);
        }
        else {
            query = entityManager.createQuery(sql1);
        }
        query.setParameter("cc", count.getCommentCount());
        query.setParameter("dc", count.getDownloadCount());
        query.setParameter("bc", count.getBrowserCount());
        query.setParameter("dlc", count.getDislikeCount());
        query.setParameter("lc", count.getLikeCount());
        query.setParameter("s", count.getScore());
        query.setParameter("r1", count.getReserve1());
        query.setParameter("r2", count.getReserve2());
        query.setParameter("etype", count.getEntityType());
        query.setParameter("eid", count.getEntityId());
        int c = query.executeUpdate();
        if (c == 0) {
            entityManager.persist(count);
        }
    }

    @Cacheable (value = "module.cms.ccount.l", key = "#criteria.toString()")
    public List<ContentCount> listCount(Criteria criteria)
    {
        return SqlUtils.sqlQueryWithoutCount(SQL_COUNT, entityManager, criteria);
    }

    protected static final String SQL_COUNT = "SELECT contentCount FROM com.jade.modules.cms.ContentCount contentCount";
}
